package com.xiaoyy.app.extend.integration.impl;

import com.alibaba.gov.api.client.AtgBusClient;
import com.alibaba.gov.api.client.DefaultAtgBusClient;
import com.alibaba.gov.api.domain.AtgBusSecretKey;
import com.alibaba.gov.api.request.AtgBizMessageSendMessageRequest;
import com.alibaba.gov.api.response.AtgBizMessageSendMessageResponse;
import com.xiaoyy.app.extend.common.vo.zlbsendmsg.ZLBSendMSGVO;
import com.xiaoyy.app.extend.integration.ZLBFuncDockingIntegration;
import com.xiaoyy.core.common.exception.AppException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 浙里办功能对接
 */
@Service
public class ZLBFuncDockingIntegrationImpl implements ZLBFuncDockingIntegration {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());
    private AtgBusClient atgBusClient;
    private String appId;


    @Override
    public void sendMessage(ZLBSendMSGVO vo) {
        AtgBizMessageSendMessageResponse response = send(vo);
        String success = response.getResultStatus();
        if(!"S".equals(success)){
            throw new AppException("调用浙里办通知接口错误："+response.getResultMsg());
        }
    }

    public void doInitAtgBusClient(){
        appId = "3002008301";
        String url = "http://59.202.52.125:8086/openapi/webapi/atgmessage/message/send";
        String keyId = "5zczv8g51flu22j1hnk988tx6iuof0wg";
        String secretKey = "=iCL+Mh6f5+gbJWir=4GcWI6";
        //总线注册获取的key secret
        AtgBusSecretKey atgBusSecretKey = new AtgBusSecretKey(keyId, secretKey);
        /**
         * 秘钥信息
         */
        List<AtgBusSecretKey> secretKeys = new ArrayList<AtgBusSecretKey>();
        secretKeys.add(atgBusSecretKey);
        atgBusClient = new DefaultAtgBusClient(url, appId, secretKeys);
    }

    public AtgBizMessageSendMessageResponse send(ZLBSendMSGVO vo){
        doInitAtgBusClient();
        AtgBizMessageSendMessageRequest request = new AtgBizMessageSendMessageRequest();
        //总线注册的应用id
        request.setAppId(appId);
        //消息的唯一id，用于幂等
        if(StringUtils.isEmpty(vo.getRequestId())){
            request.setRequestId(UUID.randomUUID().toString());
        }else{
            request.setRequestId(vo.getRequestId());
        }
        //具体通知业务编码，获取可联系开发人员
        request.setServiceCode(vo.getServiceCode());
        //如果可以获取易和id，则使用易和id，否则按如下规则业务编码+身份证号
        request.setToUserId(vo.getServiceCode()+vo.getIdcard());
        //假设消息模板配置如下：{"title":"【请给我们的服务打分，您有一个办结件等待您的评价！】","text":"【您于$affairTime，办理了《$matterName》。请给本次服务打分，以帮助我们持续改进服务质量，提高服务水平。】"}
        Map<String, String> templateArgs = vo.getTemplateArgs();
        //linkUrl
        //配置后，发送的消息内容即为"您于2019-09-23 09:00:00，办理了《驾驶证业务》。请给本次服务打分，以帮助我们持续改进服务质量，提高服务水平。"

        //渠道配置取值
        //如通知渠道为浙里办，当前发送接口支持对易和id和身份证的参数，若为其他渠道，需与开发对接
        Map<String, String> receiver = new HashMap<String, String>();

        //身份证（填入真实的身份证号）
        receiver.put("id_card", vo.getIdcard());

        request.setReceiver(receiver);
        request.setTemplateArgs(templateArgs);

        AtgBizMessageSendMessageResponse response = atgBusClient.execute(request);
        return response;
    }
}
